package com.topvision.jack.camerasdk2.admin;


import android.content.Context;
import android.hardware.Camera;
import com.topvision.jack.camerasdk2.camera.callback.TakePhotoCallback;
import com.topvision.jack.camerasdk2.camera.utils.FileUtils;
import com.topvision.jack.camerasdk2.camera.view.CameraSurfaceView;
import com.topvision.jack.camerasdk2.camera.utils.CameraUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Camera sdk
 * 创建时间:2017/12/20
 * 作者 jack
 * 描述
 */
public class TvCameraSdk {
    public final String TAG = TvCameraSdk.class.getSimpleName();
    //单例模式
    private static TvCameraSdk instance;
    Camera mCamera = null;

    private TvCameraSdk() {
    }

    public static TvCameraSdk getInstance() {
        if (instance == null) {
            synchronized (TvCameraSdk.class) {
                if (instance == null) {
                    instance = new TvCameraSdk();
                }
            }
        }
        return instance;
    }


    /**
     * 开启摄像头
     * 创建时间:2017/10/13
     * 作者 jack
     * 描述
     */
    public Camera openCamera(Context context, int cameraId) {
        try {
            mCamera = Camera.open(cameraId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mCamera;
    }


    /**
     * 设置最优分辨率
     *
     * @param context 上下文
     * @return
     */
    public void setBestPreviewSize(Context context) {
        if (mCamera != null) {
            Camera.Parameters parameters = mCamera.getParameters();
            Camera.Size size = CameraUtils.findBestPreviewSize(context, mCamera, parameters);
            if (size != null) {
                parameters.setPreviewSize(size.width, size.height);
                mCamera.setParameters(parameters);
            }
        }
    }

    public void setPreviewSize(Context context, int previewWidth, int previewHeight) {
        if (mCamera != null) {
            Camera.Parameters parameters = mCamera.getParameters();
            if (CameraUtils.isSupportPreviewSize(context, parameters, previewWidth, previewHeight)) {
                parameters.setPreviewSize(previewWidth, previewHeight);
                mCamera.setParameters(parameters);
            }
        }
    }

    public void setPictureSize(Context context, int previewWidth, int previewHeight) {
        if (mCamera != null) {
            Camera.Parameters parameters = mCamera.getParameters();
            if (CameraUtils.isSupportPictureSize(context, parameters, previewWidth, previewHeight)) {
                parameters.setPictureSize(previewWidth, previewHeight);
                mCamera.setParameters(parameters);
            }
        }
    }


    /**
     * 开始录制
     *
     * @param cameraSurfaceView 录制具体实现类
     * @param resolution        录制视频的分辨率
     * @param outputFilePath    录制文件输出目录
     */
    public void startRecord(Context context, CameraSurfaceView cameraSurfaceView, String outputFilePath, int resolution) {
        cameraSurfaceView.startRecord(context, outputFilePath, resolution);
    }


    /**
     * 停止录制
     */
    public void stopRecord(CameraSurfaceView cameraSurfaceView) {
        cameraSurfaceView.stopRecord();
    }

    //暂停录制
    public void pauseRecord(CameraSurfaceView cameraSurfaceView, String outputPath) {
        cameraSurfaceView.pauseRecord(outputPath);
    }

    //继续录制
    public void resumeRecord(CameraSurfaceView cameraSurfaceView) {
        cameraSurfaceView.resumeRecord();
    }


    //拍照
    public void takePhoto(Context context, String path) {
        if (mCamera != null) {
            mCamera.takePicture(null, null, new TakePhotoCallback(context, path));
        }
    }

    //删除视频或图片
    public boolean deleteVideoOrPhoto(Context context, String path) {
        return FileUtils.removeFile(context, path);
    }

    //批量删除
    public void deleteVideoOrPhoto(Context context, List<String> paths) {
        for (String path : paths) {
            FileUtils.removeFile(context, path);
        }
    }

}
